// import {createRouter, createWebHistory} from 'vue-router'
import {createRouter, createWebHashHistory} from 'vue-router'
import {getPageCount, setPageCount} from '../util/countStore';
import {recordData} from '../apis/record'
import WorkBench from '../views/workBench.vue'
import RankProduct from '../views/rankProduct.vue'
import RankProduct_1 from '../views/rankProduct_1.vue'
import RankProduct2 from '../views/rankProduct2.vue'
import RankProduct2_1 from '../views/rankProduct2_1.vue'
import RankProduct2_2 from '../views/rankProduct2_2.vue'
import RankProduct2_3 from '../views/rankProduct2_3.vue'

import PricePlan from '/src/views/pricePlan.vue';
import ReserveLog from '/src/views/reserveLog.vue'
import MineSetting from '../views/mineSetting.vue'
import PasswordSetting from '../views/passwordSetting.vue'
import PaySetting from '../views/paySetting.vue'

import ProductDetail2 from '../views/productDetail2.vue'
import ProductDetail3 from '../views/productDetailTemu.vue'
import LoginPage from '../views/loginPage.vue'
import RegisterPage from '../views/registerPage.vue'
import Cookies from 'js-cookie'

const routes = [
  {
    path: '/',
    redirect:'/panel',
    children:[
      {
        path:'/panel',
        redirect:'/panel/work',
        children:[{
          path:'work',
          name:'work',
          component: WorkBench,
          meta: {
            keepAlive: true,
            gtm: 'WorkBench | LinkBuy'
          }
        },
        {
          path:'rank',
          name:'rank',
          component: RankProduct,
          meta: {
            keepAlive: true,
            gtm: 'RankHotSelling'
          }
        },{
          path:'rank_1',
          name:'rank_1',
          component: RankProduct_1,
          meta: {
            keepAlive: true,
            gtm: 'RankGrowthRate'
          }
        },{
          path:'rank2',
          name:'rank2',
          component: RankProduct2,
          meta: {
            keepAlive: true,
            gtm: 'RankHotProducts'
          }
        },{
          path:'rank2_1',
          name:'rank2_1',
          component: RankProduct2_1,
          meta: {
            keepAlive: true,
            gtm: 'RankDailySales'
          }
        },{
          path:'rank2_2',
          name:'rank2_2',
          component: RankProduct2_2,
          meta: {
            keepAlive: true,
            gtm: 'RankHotNewProducts'
          }
        },{
          path:'rank2_3',
          name:'rank2_3',
          component: RankProduct2_3,
          meta: {
            keepAlive: true,
            gtm: 'RankNewStore'
          }
        },{
          path:'detail2',
          name:'detail2',
          component:ProductDetail2,
          meta: {
            keepAlive: true,
            gtm: 'DetailShein'
          }
        },
        {
          path:'detail3',
          name:'detail3',
          component:ProductDetail3,
          meta: {
            keepAlive: true,
            gtm: 'DetailTemu'
          }
        },
      ] 
      },
      {
        path:'/subscribe',
        redirect:'/subscribe/price',
        children:[
          {
            path:'price',
            name:'price',
            component: PricePlan,
            meta: {
              keepAlive: true,
              gtm: 'PricePlan'
            }
          },
          {
            path:'reserve',
            name:'reserve',
            component: ReserveLog,
            meta: {
              keepAlive: true,
              gtm: 'ReserveLog'
            }
          }
        ]
      },
      {
        path:'/setting',
        redirect:'/setting/mine',
        children:[
          {
            path:'mine',
            name:'mine',
            component: MineSetting,
            meta: {
              keepAlive: true,
              gtm: 'SetUserInfo'
            }
          },
          {
            path:'password',
            name:'password',
            component: PasswordSetting,
            meta: {
              keepAlive: true,
              gtm: 'SetPassword'
            }
          },
          {
            path:'pay',
            name:'pay',
            component: PaySetting,
            meta: {
              keepAlive: true
            }
          }
        ]
      }
    ]
  },
  {
    path:'/login',
    name:'login',
    component:LoginPage,
    meta: {
      keepAlive: false,
      gtm: 'Login'
    }
  },
  {
    path:'/register',
    name:'register',
    component:RegisterPage,
    meta: {
      keepAlive: false,
      gtm: 'Register'
    }
  }
]

const router = createRouter({
  history:createWebHashHistory(),
  // history: createWebHistory('/data'),
  routes,
})
// 全局前置守卫，以拦截路由变化并更新计数
router.beforeEach((to, from, next) => {
  if (to.meta && to.meta.gtm) {
    document.title = to.meta.gtm; // 设置页面标题
  }
  // 获取当前页面
  const pagePath = to.meta.gtm;
  // 获取当前页面的访问次数
  let {count} = getPageCount(pagePath);

  // 如果是首次访问，则计数加一
  if(count === 0){
    count = 1
  }else{
    count++;
  }
  // 检查并更新 expires 参数
  const currentExpires = Cookies.get('clickRecordExpires');
  if (currentExpires === '1') {
    Cookies.set('clickRecordExpires', '365');
  }
  // 更新计数到存储
  setPageCount(pagePath, count);
  // 获取来源页面URL和当前页面URL
  let fromUrl = from.fullPath;
  // // 解析 URL 中的查询参数
  // const urlParams = new URLSearchParams(window.location.search);
  // const referrer = urlParams.get('returnUrl')
  // if(referrer){
  //   // 如果有 'referrer' 参数，使用其值作为来源页面URL
  //   fromUrl = referrer
  // }
  const toUrl = to.fullPath;
  // 存储来源页面URL和当前页面URL到cookie
  Cookies.set('fromPageUrl', fromUrl);
  Cookies.set('currentPageUrl', toUrl);
  // 可选：将计数添加到路由元信息中，以便在组件中使用
  to.meta.pageCount = count;
  // pv:count 不传次数
  recordData({current_url:toUrl,
    from_url:fromUrl,
    fingerprint:Cookies.get('fingerprint'),
    }).then((res)=>{
    if (res.code === 200 && res.status === 1){
      console.log('Data reported successfully');
    }else{
      console.error('Error reporting data:', res);
    }
  })
  next();
})

export default router
